#!/bin/bash
#
# name: dropbear.sh
# auth: doctype <https://t.me/ndiey>
# desc: install, uninstall, add port, edit port, delete port
#       dropbear package service.
##
clear

. /etc/os-release
. /etc/environment
if [ "${EUID}" -ne 0 ]; then
    echo "You need to run this script as root" && exit 1
fi

function menu() {
    echo "============================================="
    echo "Option lists:"
    echo " [01] install      - Pasang kembali pakej dropbear"
    echo " [02] uninstall    - Padam pakej dropbear dari sistem"
    echo " [03] configure    - Edit semula fail konfig"
    echo " [04] detail       - Edit semula fail konfig"
    echo " [00] quit         - Keluar dari menu"
    echo "============================================="
    read -p "Masukkan pilihan anda [00-04]: " CHOOSE
    case "$CHOOSE" in
        01|install)
            install && break
        ;;
        02|uninstall)
            uninstall && break
        ;;
        03|configure)
            configure && break
        ;;
        04|detail)
            configure && break
        ;;
        00|quit)
            exit 0
        ;;
        *)
            clear
            echo "You have entered invalid option!"
            menu
        ;;
    esac
}

function install() {
    echo "Hello, by doing this i will installing dropbear package"
    echo "again with the default configuration setup."
    read -p "Continue with the installtion? " YESNO
    if [[ "$YESNO" = ^[Yy]$ ]]; then
        echo -n "installing dropbear package... "
        apt-get -y -qq autoremove --purge dropbear >/dev/null
        apt-get -y -qq autoremove >/dev/null
        sleep 3
        apt-get -y -qq install dropbear >/dev/null
        echo "[DONE]"
    fi
}

function configure() {
    echo "By default service not enabled on the system startup."
    read -p "Enabled service? " YESNO
    if [[ "$YESNO" = ^[Yy]$ ]]; then
        echo -n "Enabled service on system startup... "
        sed -i "s/NO_START=0/NO_START=1/" /etc/default/dropbear
        echo "[DONE]"
    fi
    echo "";
    echo "By default dropbear service listening on port 22."
    read -p "Do you want to change port?" YESNO
    if [[ "$YESNO" = ^[Yy]$ ]]; then
        until [[ "$DBPORT" =~ ^[0-9]+$ ]]; do
            read -p "Enter new port: " DBPORT
            sed -i "s/DROPBEAR_PORT=22/DROPBEAR_PORT=${DBPORT}/" /etc/default/dropbear
        done
    fi
    echo "";
    echo "By default dropbear service only use one port."
    read -p "Do you want to add new port?" YESNO
    if [[ "$YESNO" = ^[Yy]$ ]]; then
        echo "If you want to add more than one port, you need to seperate"
        echo "them with [space]. Maxsimum ports can be add is three."
        read -p "Enter new ports: " EXTDBPORT1 EXTDBPORT2 EXTDBPORT3
        if [[ -n "$EXTDBPORT1" && "$EXTDBPORT1" =~ ^[0-9]+$ ]]; then
            sed -i "s/DROPBEAR_EXTRA_ARGS=\"\"/DROPBEAR_EXTRA_ARGS=\"-p ${EXTDBPORT1}\"/" /etc/default/dropbear
        fi
        if [[ -n "$EXTDBPORT1" && -n "$EXTDBPORT2" && "$EXTDBPORT2" =~ ^[0-9]+$ ]]; then
            sed -i "s/DROPBEAR_EXTRA_ARGS="-p ${EXTDBPORT1}"/DROPBEAR_EXTRA_ARGS=\"-p ${EXTDBPORT1} -p ${EXTDBPORT2}\"/" /etc/default/dropbear
        fi
        if [[ -n "$EXTDBPORT1" && -n "$EXTDBPORT2" && -n "$EXTDBPORT3" && "$EXTDBPORT3" =~ ^[0-9]+$ ]]; then
            sed -i "s/DROPBEAR_EXTRA_ARGS="-p ${EXTDBPORT1} -p ${EXTDBPORT2}"/DROPBEAR_EXTRA_ARGS=\"-p ${EXTDBPORT1} -p ${EXTDBPORT2} -p ${EXTDBPORT3}\"/" /etc/default/dropbear
        fi
    else
        break && exit 0
    fi
}

function uninstall() {
    read -p "Continue with the uninstallation? " YESNO
    if [[ "$YESNO" == ^[Yy]$ ]]; then
        echo -n "Removing dropbear package... "
        apt-get -y -qq autoremove --purge dropbear >/dev/null
        apt-get -y -qq autoremove >/dev/null
        echo "[DONE]"
    fi
}

function detail() {
    local isActive=$(systemctl is-active dropbear.service)
    local isEnable=$(systemctl is-enabled dropbear.service)
    local listDeps=$(systemctl list-dependencies dropbear.service)
    local unitID=$(systemctl show dropbear.service -p Id)
    local unitName=$(systemctl show dropbear.service -p Names)
    local unitDesc=$(systemctl show dropbear.service -p Description)
    
    echo "";
    echo "Dropbear service detail"
    echo "ID: $unitID"
    echo "Names: $unitName"
    echo "Description: $unitDesc"
    echo "Listening: $DBPORT $EXTDBPORT1 $EXTDBPORT2 $EXTDBPORT3";
    if [[ "$isActive" -eq 0 ]]; then
        echo " Dropbear service is: Active"
    fi
    if [[ "$isEnable" -eq 0 ]]; then
        echo " Dropbear service is: Enabled"
    fi
    echo "$listDeps"
    echo "";
    echo "Start service: systemctl start dropbear"
    echo "Stop service: systemctl stop dropbear"
    echo "Restart service: systemctl restart dropbear"
    echo "Enable service: systemctl enable dropbear"
    echo "Disable service: systemctl disable dropbear"
    echo "";
}

if [[ -f /etc/default/dropbear ]]; then
    menu
elif [[ ! -f /etc/default/dropbear ]]; then
    install
fi
